#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QLabel>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QGraphicsScene>
#include <QGraphicsView>

#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    //init
    //rowPos = {25, 70, 125, 170};
    rowPos[0] = 25;
    rowPos[1] = 70;
    rowPos[2] = 125;
    rowPos[3] = 170;
    // row -

    ui->setupUi(this);

    this->setFixedSize(640, 320);

    m_showLabel = new QLabel("输入 ");
    m_inputLineEdit = new QLineEdit;
    m_inputLineEdit->setFocus();

    m_clickButton = new QPushButton("BitMap");

    m_buttonlayout = new QHBoxLayout;
    m_buttonlayout->addWidget(m_showLabel);
    m_buttonlayout->addWidget(m_inputLineEdit);
    m_buttonlayout->addWidget(m_clickButton);


    m_pScene = new QGraphicsScene;
    m_pView = new QGraphicsView;
    m_pView->setScene(m_pScene);
    m_pView->setSceneRect(0, 0, 180, 180);

    m_mainLayout = new QVBoxLayout;
    m_mainLayout->addLayout(m_buttonlayout);
    m_mainLayout->addWidget(m_pView);

    //m_pView->show(); //m_pView not show;

    ui->centralWidget->setLayout(m_mainLayout);
    setItem();// 设置item位置

    connect(m_clickButton, SIGNAL(clicked()), this, SLOT(clickButtonSlot(QString)));
    //输入信息为空。则恢复颜色
   // connect(m_inputLineEdit, SIGNAL(textChanged(QString)), this, SLOT(colorRecovery(QString)));
    connect(m_inputLineEdit, SIGNAL(textChanged(QString)), this, SLOT(clickButtonSlot(QString)));
    connect(m_inputLineEdit, SIGNAL(returnPressed()), this, SLOT(clickButtonSlot(QString)));

   // Hex to bin -- ch
//    int c= 0xb;
//    int a[4];
//    for(int i = 0 ; i !=4 ; i++)
//    {
//        a[4 - 1 -i] = c % 2;
//        c /= 2;
//    }
}

MainWindow::~MainWindow()
{
    delete ui;
}


// Item set
void MainWindow::setItem()
{
    // set Num 64;
    unsigned row = -1;
    unsigned colnum = 1;
    for(int i=1; i<=64; i++)
    {
        if(i % 16 == 1)
        {
            row++;
            colnum = 1;
        }
        else
            colnum++;

        if(i % 8 == 1 && 1 !=i && 17 != i && 33 != i && 49 != i )
            colnum++;

        YLCrea *el = new YLCrea(i);
        m_pScene->addItem(el);
        m_YLList.append(el);

        int movebit = 30 * colnum;
        el->setPos(-180 + movebit, rowPos[row]);
    }
}

//private slots:
void MainWindow::clickButtonSlot(QString inputstr)
{
  //QString inputstr =  m_inputLineEdit->text();
  QString Hexstr;
  //  去掉空白字符;
  for(QString::iterator iter= inputstr.begin(); iter != inputstr.end(); iter++)
  {
      if(!iter->isSpace())
           Hexstr.append(*iter);
  }

  QByteArray t_ba;
  t_ba.append(Hexstr).toHex();// toHex;
  //qDebug() << "toHex t_ba = " << t_ba << endl; // -- 转成16进制的数据； 此时的数据是可以使用的；

  // bHex  to bin
  QChar ch[4];
  QString binstr;
  if(t_ba.size() > 16)
      return;

  for(QByteArray::iterator iter=t_ba.begin(); iter!=t_ba.end(); iter++)
  {
      //qDebug() << "Item = " << *iter << endl; // -- 获得的单独数据是真正确的；
      if(*iter != '0')
      {
        int tmp_num = 0; // init
        if( *iter <= '9' && *iter >= '0')
             tmp_num = *iter - '0';
        else if(*iter <='f' && *iter >='a')
             tmp_num =  *iter - 'a' + 9 + 1;
        else if(*iter <= 'F' && *iter >= 'A')
            tmp_num = *iter - 'A' + 9 + 1;
        else
            return;

        for(int i =0 ; i!=4; i++)
        {
            ch[4-1-i] = tmp_num % 2;
            tmp_num /= 2;
        }
        for(int i=0; i!=4; i++)
        {
           binstr.append(QString::number(ch[i].toLatin1()));
        }
      }
      else
      {
          binstr.append("0000");
      }
  }


  // 变色
  unsigned  num = 0;
  for(QString::iterator iter = binstr.begin(); iter!=binstr.end(); ++iter)
  {
      if(*iter == '1')
         m_YLList.at(num)->setFlash(true);
      num++;
  }
  for(num=binstr.size(); num < m_YLList.size(); num++)
  {
      m_YLList.at(num)->setFlash(false);
  }
}



YLCrea::YLCrea(unsigned int t_num)
{
    m_Num = t_num;
    setFlash(false); // set bool .. green
}

YLCrea::~YLCrea()
{


}


//
void YLCrea::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    painter->setRenderHint(QPainter::Antialiasing, true); // 反走样
    painter->setPen(Qt::blue);
    painter->setBrush( flash ? (Qt::red) : (Qt::green) );
    painter->drawEllipse(-25, -25, 25, 25);


    painter->drawText(QRectF(-25, -25, 25, 25), Qt::AlignCenter,QString::number(m_Num));


}
